﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Tele.DataModels;
using Tele.db;
using Tele.db.IDataAdapters.Adapters;
using Tele.db.IDataAdapters.Interfaces;

namespace Tele.Controllers
{
    public class CharactersController : Controller
    {
        ITVShowAdapter _adapter;
        public CharactersController()
        {
            _adapter = new TVShowAdapter();
        }
        public CharactersController(ITVShowAdapter adapter)
        {
            _adapter = adapter;
        }
        public ActionResult Index()
        {
            List<Character> characters = _adapter.GetAllCharacters();
            return View(characters);
        }
        public ActionResult DetailsCharacter(int id)
        {
            Character character = _adapter.GetCharacter(id);
            return View(character);
        }
        public ActionResult AddCharacter()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddCharacter(int id, string name, string photo)
        {
            Character character = new Character();
            character.Name = name;
            character.Photo = photo;
            character.TVShowId = id;
            character = _adapter.AddCharacter(character);
            return RedirectToAction("DetailsTVShow/" + character.Id);
        }
        public ActionResult DeleteCharacter(int id)
        {
            Character character;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Find(id);
                db.Characters.Remove(character);
                db.SaveChanges();
            }
            return RedirectToAction("AddCharacter", new { id = character.TVShowId });
        }
        [HttpPost]
        public ActionResult UpdateCharacter(string name, string photo, int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Character oldchar = db.Characters.Find(id);
                oldchar.Name = name;
                oldchar.Photo = photo;
                db.SaveChanges();
            }
            return RedirectToAction("DetailsTVShow/" + id);
        }
    }
}
